

%% load panel data
load('../../baseline_model/data_panel.mat')
direc=readtable('majorPowerInterventions.csv');
direc=direc(direc.year>=50&direc.year<=99,:);
% code neutral interventions as abstentions
direc.direct(direc.direct==3)=0;


%% aggregate data for entire period, 1950-1999

countries=unique(data_panel.cown);

data=data_panel(1:length(countries),[1:3,6:end]);
for n=1:length(countries)
    dn=data_panel(data_panel.cown==countries(n),:);
    data(n,1:3)=dn(1,1:3);
    data(n,4:end)=array2table(nanmean(table2array(dn(:,6:end))));
end

outcomes=outcomes_panel(1:length(countries),[1:3,5:end]);
for n=1:length(countries)
    yn=outcomes_panel(outcomes_panel.cown==countries(n),:);
    dn=direc(direc.target==countries(n),:);
    outcomes(n,1:3)=yn(1,1:3);
    outcomes.War(n)=1*(sum(yn.War)>0);
    outcomes(n,5:end)=array2table(zeros(1,5));
    if sum(dn.mp==2)>0
        outcomes.US(n)=mode(dn.direct(dn.mp==2));
    end
    if sum(dn.mp==200)>0
        outcomes.UK(n)=mode(dn.direct(dn.mp==200));
    end
    if sum(dn.mp==220)>0
        outcomes.France(n)=mode(dn.direct(dn.mp==220));
    end
    if sum(dn.mp==365)>0
        outcomes.Russia(n)=mode(dn.direct(dn.mp==365));
    end
    if sum(dn.mp==710)>0
        outcomes.China(n)=mode(dn.direct(dn.mp==710));
    end
end

missing=find(~isfinite(data.gdp_pc)|~isfinite(data.pop)...
    |~isfinite(data.polity)|~isfinite(data.USally));
data(missing,:)=[];
outcomes(missing,:)=[];


%%
clearvars -except data outcomes
save('data_direction.mat')

